<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 论坛接口
 */
class Forum extends Api
{
    protected $noNeedLogin = ['page'];
    protected $noNeedRight = ['*'];

    /**
     * 发布
     *
     */
    public function add()
    {
        $data = [
            'user_id'=>$this->auth->id,
            'forum_name'=>input('forum_name',''),
            'type'=>input('type',0),
            'forum_content'=>input('forum_content',''),
            'images'=>input('images',''),
            'deploy_id'=>input('deploy_id',0),
        ];
        if (empty($data['forum_name'])) {
           $this->error('请输入标题');
        }
        if (empty($data['forum_content'])) {
            $this->error('请输入内容');
        }


        $forum_model = new \app\api\model\forum\Forum();

        $res = $forum_model->save($data);
        \app\api\model\Diy::where(['id'=>$data['deploy_id']])->update(['forum_id'=>$forum_model->forum_id]);
        $this->success('请求成功',$res);
    }

    //论坛大厅
    public function page()
    {
        $keyword = input('keyword','');

        //$order = input('order','');
        $type = input('type','all');
        $where = [];
        if ($type!='all'){
            $where['f.type'] = $type;
        }
        /*$where = [
            'type'=>$type
        ];*/

        if ($keyword){
            $where['f.forum_name|f.forum_content'] = ['like', '%' . $keyword . '%'];
        }

        /*if ($order=='new'){
            $orderBy = 'f.createtime desc';
        } elseif ($order=='hot'){
            $orderBy = 'f.recommend_num desc';
        } else {
            $orderBy = '';
        }*/
        $orderBy = 'f.is_top desc f.createtime desc';
        $list = \app\api\model\forum\Forum::alias('f')
            ->join('user u', 'u.id=f.user_id', 'LEFT')
            ->join('diy d', 'd.id=f.deploy_id', 'LEFT')
            ->field('f.*,u.nickname,u.avatar,u.level,u.level,d.all_prices')
            ->where($where)
            ->order($orderBy)
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('请求成功',$list);
    }

    public function addrepost()
    {
        $forum_id = input('forum_id',0);
        \app\api\model\forum\Forum::where(['forum_id'=>$forum_id])->setInc('repost_num',1);
        $this->success('');
    }

    //我的帖子
    public function my()
    {
        $keyword = input('keyword','');

        $where = ['f.user_id'=>$this->auth->id];

        if ($keyword){
            $where['f.forum_name|f.forum_content'] = ['like', '%' . $keyword . '%'];
        }

        $list = \app\api\model\forum\Forum::alias('f')
            ->join('user u', 'u.id=f.user_id', 'LEFT')
            ->field('f.*,u.nickname,u.avatar,u.level')
            ->where($where)
            ->order('f.createtime desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('请求成功',$list);
    }

    //帖子详情
    public function detail()
    {
        $forum_id = input('forum_id',0);

        $detail = \app\api\model\forum\Forum::alias('f')
            ->join('user u', 'u.id=f.user_id', 'LEFT')
            ->where(['f.forum_id'=>$forum_id])
            ->field('f.*,u.nickname,u.avatar,u.level')
            ->find();

        $this->success('请求成功',$detail);
    }

    //上传装机内容
    public function uploadDeploy()
    {
        $forum_id = input('forum_id',0);

        $deploy_images = input('deploy_images','');

        $deploy_content = input('deploy_content','');

        $detail = \app\api\model\forum\Forum::get($forum_id);

        $detail->deploy_images = $deploy_images;

        $detail->deploy_content = $deploy_content;

        if ($detail->save()){
            $this->success('上传成功');
        } else {
            $this->error('上传失败');
        }

    }

    //帖子排行榜前三名
    public function rankingList()
    {
        $list = \app\api\model\forum\Forum::order('recommend_num desc')
            ->field('forum_id,forum_name,forum_content,recommend_num')
            ->limit(3)->select();

        $this->success('请求成功',$list);
    }

    //排行榜
    public function ranking()
    {
        $list = \app\api\model\forum\Forum::alias('f')
            ->join('user u', 'u.id=f.user_id', 'LEFT')
            ->field('f.recommend_num,u.nickname,u.avatar,u.level')
            ->order('f.recommend_num desc')
            ->select();

        $this->success('请求成功',$list);
    }

}
